package com.iweibo.ui;

import com.iweibo.R;
import android.os.Bundle;
import android.app.TabActivity;
import android.content.Intent;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

/**
 * 主框架Activity
 * @author nibo
 * @date 2012-11-21
 *
 */
public class MainActivity extends TabActivity {

	//主页
	private static final String HOME_TAB = "home";
	//提及
	private static final String AT_TAB = "at";
	//信息
	private static final String MSG_TAB = "MSG";
	//更多
	private static final String MORE_TAB = "more";
	
	private static final String TAG = "MainActivity";
	
	private TabHost tabHost = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Intent userInfoIntent = getIntent();
        long userId = userInfoIntent.getLongExtra("user_id" , -1);
        String userName = userInfoIntent.getStringExtra("user_name");
        String token = userInfoIntent.getStringExtra("token");
        int expiresIn = userInfoIntent.getIntExtra("expires_in", -1);
        
        tabHost = this.getTabHost();
        Intent homeIntent = new Intent(this, HomeActivity.class);
        homeIntent = setUserIntent(homeIntent, userId, userName, token, expiresIn);
        TabSpec homeSpec = tabHost.newTabSpec(HOME_TAB).setIndicator(HOME_TAB).setContent(homeIntent);
        Intent atIntent = new Intent(this, AtActivity.class);
        atIntent = setUserIntent(atIntent, userId, userName, token, expiresIn);
        TabSpec atSpec = tabHost.newTabSpec(AT_TAB).setIndicator(AT_TAB).setContent(atIntent);
        Intent newIntent = new Intent(this, MsgActivity.class);
        newIntent = setUserIntent(newIntent, userId, userName, token, expiresIn);
        TabSpec msgSpec = tabHost.newTabSpec(MSG_TAB).setIndicator(MSG_TAB).setContent(newIntent);
        Intent moreIntent = new Intent(this, MoreActivity.class);
        moreIntent = setUserIntent(moreIntent, userId, userName, token, expiresIn);
        TabSpec moreSpec = tabHost.newTabSpec(MORE_TAB).setIndicator(MORE_TAB).setContent(moreIntent);
        
        tabHost.addTab(homeSpec);
        tabHost.addTab(atSpec);
        tabHost.addTab(msgSpec);
        tabHost.addTab(moreSpec);
        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg_main_btns);
        radioGroup.setOnCheckedChangeListener(mainRadioBtnListener);
        
    }
    
    private Intent setUserIntent(Intent intent, long userId, String userName, String token, int expiresIn) {
    	intent.putExtra("user_id", userId);
    	intent.putExtra("user_name", userName);
    	intent.putExtra("token", token);
    	intent.putExtra("expires_in", expiresIn);
    	return intent;
    }
    
    /**主页,提及,信息,更多切换事件*/
    private OnCheckedChangeListener mainRadioBtnListener = new OnCheckedChangeListener() {
		
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch(checkedId) {
				case R.id.rd_home:
					tabHost.setCurrentTabByTag(HOME_TAB);
					break;
				case R.id.rd_at:
					tabHost.setCurrentTabByTag(AT_TAB);
					break;
				case R.id.rd_msg:
					tabHost.setCurrentTabByTag(MSG_TAB);
					break;
				case R.id.rd_more:
					tabHost.setCurrentTabByTag(MORE_TAB);
					break;
			}
		}
	};
    
    
}
